var gulp = require('gulp');
var del = require('del');
/* 下面是插件库 */
var sass = require('gulp-sass');//解析sass
var cleanCss = require('gulp-clean-css');//压缩css文件的工具
var uglify = require('gulp-uglify');//压缩js文件夹的.js文件
var htmlMin = require('gulp-htmlmin');//压缩html文件
var livereload = require('gulp-livereload'); //热更新
// var connect = require('gulp-connect');//热更新服务
var imageMin = require('gulp-imagemin');//压缩图片
var fontSpider = require('gulp-font-spider');//压缩字体文件
var runSequence = require('run-sequence');// 按循序执行事务
var gulpIf = require('gulp-if');//判断
var pug = require('gulp-pug');//编译pug
var browserSync = require('browser-sync').create();
// var rev = require('gulp-rev');//添加hash值
const PATH = {  //输入和输出的路径
    src :{
        css:'./src/**/*.css',
        scss :'./src/css/*.scss',
        html: './src/**/*.html',
        js:'./src/**/*.js',
        img:'./src/img/*.*',
        font:'./src/font/*.*',
        pug:'./src/pug/**/*.pug'
    },
    dest:{
        base:'./dist',
        scss:'./dist/css/scss/',
        img:'./dist/img/',
        html:'./dist/html/'
    }
}

const IS_BUILD = false;//把这个值设为true 就是压缩代码

gulp.task('css',function(){
    //处理css文件
    return gulp.src(PATH.src.css)
        .pipe(gulpIf(IS_BUILD,cleanCss({compatibility:'ie8'})))  //压缩css
        .pipe(gulp.dest(PATH.dest.base))
        .pipe(browserSync.reload({stream:true}));
});

gulp.task('scss',function(){
    //处理scss文件
    return gulp.src(PATH.src.scss)
        .pipe(sass({outputStyle: 'expanded'}))
        .pipe(gulpIf(IS_BUILD,cleanCss({compatibility:'ie8'})))  //压缩css
        .pipe(gulp.dest('./dist/css/'))
        .pipe(browserSync.reload({stream:true}));
});

gulp.task('font',function(){
    //导出字体文件
    return gulp.src(PATH.src.font)
               .pipe(gulp.dest('./dist/font/'))
               .pipe(browserSync.reload({stream:true}));
});

gulp.task('html',function(){
    //处理html页面
    return gulp.src(PATH.src.html)
            .pipe(gulpIf(IS_BUILD,htmlMin({
                minifyJS:true,
                minifyCSS:true,
                collapseWhitespace:true,
                removeComments:true,
                removeEmptyAttributes:true
            })))
            .pipe(gulp.dest(PATH.dest.base))
            .pipe(browserSync.reload({stream:true}));
});

gulp.task('img',function(){
    //处理图片
    return gulp.src(PATH.src.img)
               .pipe(imageMin({
                    interlaced: true,
                    progressive: true,
                    optimizationLevel: 5,
               }))
               .pipe(gulp.dest(PATH.dest.img))
               .pipe(browserSync.reload({stream:true}));
});

gulp.task('script',function(){
    //处理js
    return gulp.src(PATH.src.js)
               .pipe(gulpIf(IS_BUILD,uglify({ ie8:true })))
               .pipe(gulp.dest(PATH.dest.base))
               .pipe(browserSync.reload({stream:true}));
});
gulp.task('pug',function(){
    return gulp.src(PATH.src.pug)
                    .pipe(pug({
                        pretty:{}
                    }))
                    .pipe(gulp.dest(PATH.dest.html))
                    .pipe(browserSync.reload({stream:true}));
});
gulp.task('clean',function (cb) {
    return del(['./dist'],function(){
        console.log('删除缓存');
    });
});

gulp.task('dev',(done)=>{
    runSequence(
        ['scss','css','font'],['script'],['html'],['img'],['pug'],done
    )
});

gulp.task('watch',function(){
    gulp.watch(['./src/**/*.*',],['dev']);
});

gulp.task('connect',function(){
    // connect.server({
    //     port:9003,
    //     root:'dist',
    //     livereload:true
    // });
    browserSync.init({
        port:9003,
        server:{
            baseDir:['dist']
        }
    })
});

gulp.task('default',['clean','dev','connect','watch']);
